package cn.iocoder.yudao.module.kt.controller.admin.ketangwatch;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.kt.controller.admin.ketangwatch.vo.*;
import cn.iocoder.yudao.module.kt.dal.dataobject.ketangwatch.KetangWatchDO;
import cn.iocoder.yudao.module.kt.service.ketangwatch.KetangWatchService;

@Tag(name = "管理后台 - 数字人课堂观看记录")
@RestController
@RequestMapping("/kt/ketang-watch")
@Validated
public class KetangWatchController {

    @Resource
    private KetangWatchService ketangWatchService;

    @PostMapping("/create")
    @Operation(summary = "创建数字人课堂观看记录")
    @PreAuthorize("@ss.hasPermission('kt:ketang-watch:create')")
    public CommonResult<Long> createKetangWatch(@Valid @RequestBody KetangWatchSaveReqVO createReqVO) {
        return success(ketangWatchService.createKetangWatch(createReqVO));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数字人课堂观看记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('kt:ketang-watch:delete')")
    public CommonResult<Boolean> deleteKetangWatch(@RequestParam("id") Long id) {
        ketangWatchService.deleteKetangWatch(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数字人课堂观看记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('kt:ketang-watch:query')")
    public CommonResult<KetangWatchRespVO> getKetangWatch(@RequestParam("id") Long id) {
        KetangWatchDO ketangWatch = ketangWatchService.getKetangWatch(id);
        return success(BeanUtils.toBean(ketangWatch, KetangWatchRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数字人课堂观看记录分页")
    @PreAuthorize("@ss.hasPermission('kt:ketang-watch:query')")
    public CommonResult<PageResult<KetangWatchRespVO>> getKetangWatchPage(@Valid KetangWatchPageReqVO pageReqVO) {
        PageResult<KetangWatchDO> pageResult = ketangWatchService.getKetangWatchPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KetangWatchRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数字人课堂观看记录 Excel")
    @PreAuthorize("@ss.hasPermission('kt:ketang-watch:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportKetangWatchExcel(@Valid KetangWatchPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KetangWatchDO> list = ketangWatchService.getKetangWatchPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "数字人课堂观看记录.xls", "数据", KetangWatchRespVO.class,
                        BeanUtils.toBean(list, KetangWatchRespVO.class));
    }

}